package com.hrms.view;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.hrms.core.Constants;
import com.hrms.domain.Employee;
import com.hrms.domain.LastUserAction;

public abstract class View {
	
	protected static final Logger log = Logger.getLogger(TaskView.class.getName());
	
	private HttpServletRequest req;
	private HttpServletResponse res;
	private String userAction;
	private Employee accessor;
	private Map<String,String[]> parameterMap = new HashMap<String,String[]>();
	private String warnings = null;
	
	protected String getMapValue(String key) {	
		if (parameterMap.containsKey(key)) {
			return parameterMap.get(key)[0].trim();			
		}
		return null;
	}
	
	protected void log(Object msg){
		if(msg != null){
			log.log(Level.INFO, msg.toString());
		}
	}
	
	public String getWarnings() {
		return warnings;
	}

	public void setWarnings(String warnings) {
		this.warnings = warnings;
	}


	public Map<String, String[]> getParameterMap() {
		return parameterMap;
	}
	
	public void addAsAParameter(String paramName, String paramValue){
		getParameterMap().put(paramName, new String[]{ paramValue });
	}

	public void service(HttpServletRequest req, HttpServletResponse res){
		this.req = req;
		this.res = res;
		
		parameterMap.putAll(req.getParameterMap());
		userAction = getMapValue(Constants.ACTION);

		setAccessor((Employee)req.getAttribute(Employee.TYPE));
		
		addAsAParameter("userId", getAccessor().getUserId());
		
		initalize();
		persistEvents();
		
		log(getWarnings());
		getReq().setAttribute("msg", getWarnings());
		
		postPersistEvents();
		
		auditLastUserAction();
	}
	
	public void postPersistEvents(){
		//no-op
	}
	
	private void auditLastUserAction(){
		try{
			//log("aaaaaaaaaaaaaaaa"+getAccessor().getUserId());
			if(StringUtils.isNotBlank(getAccessor().getUserId())){
				new LastUserAction(getAccessor().getUserId(), new Date()).save();
			}
		}catch(Exception z){
			//ignore
		}
	}
	
	public String getReqesutedUserId(){  
			if(StringUtils.isNotEmpty(getMapValue(Constants.SELECTED_EMPLOYEE))){
				return getMapValue(Constants.SELECTED_EMPLOYEE);
			}
			return getAccessor().getUserId();
	}
	
	public boolean isSuper(){
		return getReqesutedUserId().equals(getAccessor().getUserId());
	}

	public Employee getAccessor() {
		return accessor;
	}

	public void setAccessor(Employee accessor) {
		this.accessor = accessor;
	}

	public String getUserAction() {
		return userAction;
	}

	public void setUserAction(String userAction) {
		this.userAction = userAction;
	}
	
	protected abstract void initalize();
	
	protected abstract void persistEvents();

	public HttpServletRequest getReq() {
		return req;
	}

}
